package me.itsoo.artemis.framework.repository.executor;

import me.itsoo.artemis.framework.repository.config.Configuration;
import me.itsoo.artemis.framework.repository.mapper.MapperDefinition;

import java.util.List;

/**
 * CachedSqlExecutor
 *
 * @author zxy
 * @version 0.0.1
 * @since 2023/5/28
 */
public class CachedSqlExecutor implements SqlExecutor {

    // TODO: 配置如果开启缓存，则直接

    private final SqlExecutor sqlExecutor;

    public CachedSqlExecutor(SqlExecutor sqlExecutor) {
        this.sqlExecutor = sqlExecutor;
    }

    @Override
    public Configuration getConfig() {
        return sqlExecutor.getConfig();
    }

    @Override
    public void setConfig(Configuration config) {
        sqlExecutor.setConfig(config);
    }

    @Override
    public <T> List<T> executeSelect(MapperDefinition md, BoundSql boundSql, Class<T> resultType) {
        // 这里对操作结果做缓存增强
        return null;
    }

    @Override
    public int executeUpdate(MapperDefinition md, BoundSql boundSql, ExecuteType executeType) {

        try {
            return sqlExecutor.executeUpdate(md, boundSql, executeType);
        } finally {
            // 变更操作需要删除缓存
        }
    }

    @Override
    public int executeUpdate(MapperDefinition md, BoundSql boundSql, ExecuteType executeType, boolean autoCommit) {
        try {
            return sqlExecutor.executeUpdate(md, boundSql, executeType, autoCommit);
        } finally {
            // 变更操作需要删除缓存
        }
    }
}
